var nodeCheckedSilent = false;
function nodeChecked (event, node){
    if(nodeCheckedSilent){
        return;
    }
    nodeCheckedSilent = true;
    checkAllParent(node);
    checkAllSon(node);
    nodeCheckedSilent = false;
}

var nodeUncheckedSilent = false;
function nodeUnchecked  (event, node){
    if(nodeUncheckedSilent)
        return;
    nodeUncheckedSilent = true;
    uncheckAllParent(node);
    uncheckAllSon(node);
    nodeUncheckedSilent = false;
}

//选中全部父节点
function checkAllParent(node){
    $('#grant').treeview('checkNode',node.nodeId,{silent:true});
    var parentNode = $('#grant').treeview('getParent',node.nodeId);
    if(!("nodeId" in parentNode)){
        return;
    }else{
        checkAllParent(parentNode);
    }
}
//取消全部父节点
function uncheckAllParent(node){
    $('#grant').treeview('uncheckNode',node.nodeId,{silent:true});
    var siblings = $('#grant').treeview('getSiblings', node.nodeId);
    var parentNode = $('#grant').treeview('getParent',node.nodeId);
    if(!("nodeId" in parentNode)) {
        return;
    }
    var isAllUnchecked = true;  //是否全部没选中
    for(var i in siblings){
        if(siblings[i].state.checked){
            isAllUnchecked=false;
            break;
        }
    }
    if(isAllUnchecked){
        uncheckAllParent(parentNode);
    }

}

//级联选中所有子节点
function checkAllSon(node){
    $('#grant').treeview('checkNode',node.nodeId,{silent:true});
    if(node.nodes!=null&&node.nodes.length>0){
        for(var i in node.nodes){
            checkAllSon(node.nodes[i]);
        }
    }
}
//级联取消所有子节点
function uncheckAllSon(node){
    $('#grant').treeview('uncheckNode',node.nodeId,{silent:true});
    if(node.nodes!=null&&node.nodes.length>0){
        for(var i in node.nodes){
            uncheckAllSon(node.nodes[i]);
        }
    }
}


$(document).ready(function(){
    $("#grant").dialog({
        title:"角色授权",
        btnName:"授权",
        btnBind:function(){
                var rows=$("#table").bootstrapTable("getSelections");
                var param="rid="+rows[0].id;
                var arr= $("#grant").treeview("getChecked");
                for(var i=0;i<arr.length;i++){
                    param=param+"&mids="+arr[i].id;
                }
                $.post(ctx+"sys/role/grant",param,function(msg){
                    $("#grant").dialog("close");
                   toastr.warning("信息提示",msg);

                },"text");
        }
    });
});
function prepareGrant(){
    var rows=$("#table").bootstrapTable("getSelections");
    if(rows.length==0){
        swal("请选择一项待授权!");
        return;
    }
    var param="rid="+rows[0].id;
    $.getJSON(ctx+"sys/role/tree",param,function(result){

        $('#grant').treeview({
            data: result,
            multiSelect:true,
            showCheckbox:true,
            onNodeChecked:nodeChecked ,
            onNodeUnchecked:nodeUnchecked
        });
        $("#grant").dialog("open");
    });


}


function detail(index,row){
    var param="rid="+row.id;
    var html=[];
    $.ajax({
        url:ctx+"sys/user/searchByRid",
        data:param,
        async:false,
        dataType:'json',
        success:function(arr){
            $(arr).each(function(){
                html.push('<p><b>' + this.username + ':</b> ' + this.note + '</p>');
            });
        }
    });
    return html.join("");
}
